<?php
error_reporting(0);
ini_set('memory_limit', '1000M');
set_time_limit(0);

/**
 * 根据source_url判重
 */
CONST TYPE_1 = 1;
// type字典
// 当前数据
$currList = array();
// 上一个数据
$lastList = array();
// 重复数据数组
$repeatList = array();

/** 
 *  reducer_1.php
 *      input:根据source_url排序的gov表数据，格式source_url\tTYPE\tjson(所有数据数组的格式)
 *      handle:根据source_url判重
 *      output:多路输出 1.有问题的数据直接输出,包括异常类型  #a TYPE\tjson
						2.有重复的数据只输出一条正常数据   #b json
						3.输出重复房源     #c  source_url\tjson
 * @updated 2016-01-16
 * @by lily
 */
while (($line_json = fgets(STDIN)) !== false) {   //line_json=>source_url\tjson格式
    
    $line_json = trim($line_json);
    if (empty($line_json))
        continue;
    list ($source_url, $type, $list_json) = explode("\t", $line_json);
    $type = trim($type);
    // 有问题的数据直接输出,多路输出
    if (intval($type) != 1) {
        echo $type."\t".$list_json."\n";  //#a 多路输出
        continue;
    }
    $currList['source_url'] = $source_url;   //key
    $currList['line_json'] = $list_json;     //value
    
    if ($currList['source_url'] == $lastList['source_url']) {  //重复source_url
        $repeatList[] = $currList;
    } else {
        if (! empty($lastList)) {
            if (! empty($repeatList)) { // 有重复数据出现
                //echo $repeatList[0]['line_json']."\n#b"; // #b只输出一条正常数据
				echo TYPE_1."\t".$repeatList[0]['line_json']."\n";
				//foreach($repeatList as $a)
					//echo $a['line_json']."\n#c"; // #c 输出重复房源
                $repeatList = array();
            } else { // 没有重复数据出现，输出lastList
                //echo $lastList['line_json']."\n#b";
				echo TYPE_1."\t".$lastList['line_json']."\n";
            }
        }
    }
    
    $lastList = $currList;
}

if (! empty($repeatList)) {
    //echo $repeatList[0]['line_json']."\n#b"; // 只输出一条正常数据
	echo TYPE_1."\t".$repeatList[0]['line_json']."\n";
    //foreach($repeatList as $a)
		//echo $a['line_json']."\n#c"; // 输出重复房源
}

if ($currList) {
    //echo $currList['line_json']."\n#b";
	echo TYPE_1."\t".$currList['line_json']."\n";
}



?>
